//
//  JwBaseUIModel.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-19.
//

#import "JwBaseUIModel.h"

@implementation JwBaseUIModel

+ (JSONKeyMapper *)keyMapper{
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"Id" : @"id"}];
}

+ (void)registerTableView:(UITableView *)tableView datas:(NSArray<JwBaseUIModel *> *)datas {
    NSMutableArray *cells = [NSMutableArray array];
    for (JwBaseUIModel *model in datas) {
        if (model.jw_cellName) {
            if ([cells containsObject:model.jw_cellName]) { return; }
            [self registerTableView:tableView model:model];
            [cells addObject:model.jw_cellName];
        }
    }
}

+ (void)registerTableView:(UITableView *)tableView model:(JwBaseUIModel *)model {
    if (model.jw_cellName) {
        if (model.jw_isCellXib) {
            [tableView registerNib:[UINib nibWithNibName:model.jw_cellName bundle:nil] forCellReuseIdentifier:model.jw_cellName];
        } else {
            [tableView registerClass:NSClassFromString(model.jw_cellName) forCellReuseIdentifier:model.jw_cellName];
        }
    }
}

+ (void)registerCollectionView:(UICollectionView *)collectionView datas:(NSArray<JwBaseUIModel *> *)datas {
    NSMutableArray *cells = [NSMutableArray array];
    for (JwBaseUIModel *model in datas) {
        if (model.jw_cellName || model.jw_headName || model.jw_footName) {
            if ([cells containsObject:model.jw_cellName]) { return; }
            if ([cells containsObject:model.jw_headName]) { return; }
            if ([cells containsObject:model.jw_footName]) { return; }
            [self registerCollectionView:collectionView model:model];
            if (model.jw_cellName) { [cells addObject:model.jw_cellName]; }
            if (model.jw_headName) { [cells addObject:model.jw_headName]; }
            if (model.jw_footName) { [cells addObject:model.jw_footName]; }
        }
    }
}

+ (void)registerCollectionView:(UICollectionView *)collectionView model:(JwBaseUIModel *)model {
    if (model.jw_cellName) {
        if (model.jw_isCellXib) {
            [collectionView registerNib:[UINib nibWithNibName:model.jw_cellName bundle:nil] forCellWithReuseIdentifier:model.jw_cellName];
        } else {
            [collectionView registerClass:NSClassFromString(model.jw_cellName) forCellWithReuseIdentifier:model.jw_cellName];
        }
    }
    if (model.jw_headName) {
        if (model.jw_isHeadXib) {
            [collectionView registerNib:[UINib nibWithNibName:model.jw_headName bundle:nil]
             forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:model.jw_headName];
        } else {
            [collectionView registerClass:NSClassFromString(model.jw_headName)
               forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:model.jw_headName];
        }
    }
    if (model.jw_footName) {
        if (model.jw_isFootXib) {
            [collectionView registerNib:[UINib nibWithNibName:model.jw_footName bundle:nil]
             forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:model.jw_footName];
        } else {
            [collectionView registerClass:NSClassFromString(model.jw_footName)
               forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:model.jw_footName];
        }
    }
}

@end
